package com.xzq.Hot100;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author xzq
 * @Description // 有效的括号
 * @Date 2021/12/9 22:19
 * @Version 1.0.0
 **/
public class Demo04 {
    public static void main(String[] args) {
      String  s = "]";
        boolean valid = isValid(s);
        System.out.println(valid);
    }


    public static boolean isValid(String s) {
        HashMap<Character, Character> map = new HashMap<>();
        map.put('(', ')');
        map.put('{', '}');
        map.put('[', ']');
        //栈队列，先进后出 借此一一清除括号
        ArrayDeque<Character> queue = new ArrayDeque<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                queue.addLast(c);
                continue;
            }
            if (map.containsValue(c)) {
                if (queue.isEmpty()) {
                    return false;
                }
                //判断跟队列的最后一位元素是否相补
                if (map.get(queue.getLast()) == c) {
                    //相等则队列弹出
                    queue.pollLast();
                }else{
                    return false;
                }
            }
        }
        if (!queue.isEmpty()) {
            return false;
        }
        return true;
    }
}
